require(['../config'], () => {
	require(['swiper', 'template', 'Header', 'jquery', 'bootstrap', 'footer'], (swiper, template, Header) => {
		class Cart {
			constructor() {
				this.hideee()
				this.rander()
				this.setAllCheckStatus()
				this.calcTotalMoney()
				this.checksChange()

			}
			rander() {
				let cart = localStorage.getItem('cart')
				if (cart) {
					this.cart = JSON.parse(cart)
					$('#allthing').html(template('allthingTemplate', {
						cart: this.cart
					}))
				}
				this.allCheck()
				this.delect()
				this.minus()
				this.add()
			}

			hideee() {
				let cart = localStorage.getItem('cart')
				if (cart) {
					$('.shop-empty').hide()
					$('.shop-box,.sum-box').show()
				} else {
					//购物车为空
					$('.shop-empty').show()
					$('.shop-box,.sum-box').hide()
				}
			}

			setAllCheckStatus() {
				const isAllcheck = this.cart.every(shop => shop.check)
				$('#all').prop('checked', isAllcheck)
			}

			checksChange() {
				const _this = this
				$('.check-btn').on('change', function() {
					//获取当前商品的id值
					// console.log('dfdfd')
					const id = $(this).parents('.shop-box').data('id')
					//根据id去修改里面的check的值
					_this.cart = _this.cart.map((shop) => {
						//判断shop里的id是否与当前的id相等
						if (id === shop.id) {
							//修改shop里的check的 值，让shop里的check的值等于当前的值
							shop.check = $(this).prop('checked')
						}
						//无论有没有进入if，都要返回一个shop
						return shop
					})
					localStorage.setItem('cart', JSON.stringify(_this.cart))
					//重新设置全选
					_this.setAllCheckStatus()
					//重新计算总价
					_this.calcTotalMoney()

				})
			}

			calcTotalMoney() {
				const allMoney = this.cart.reduce((money, shop) => {
					if (shop.check) {
						money += shop.count * shop.price
					}
					return money
				}, 0)
				$('.ZJ').html(allMoney.toFixed(2))
			}

			allCheck() {
				const _this = this
				$('.all').on('change', function() {
					if ($(this).prop('checked') === true) {
						$('.check-btn').prop('checked',$(this).prop('checked'))
					}else if($(this).prop('checked') === false){
						$('.check-btn').prop('checked',$(this).prop('checked'))
					}
					_this.cart = _this.cart.map((shop) => {
						shop.check = $(this).prop('checked')
						return shop
					})
					localStorage.setItem('cart', JSON.stringify(_this.cart))
					_this.calcTotalMoney()
					_this.checksChange()
				})
			}

			delect() {
				const _this = this
				$('.cancel').on('click', function() {
					const id = $(this).parents('.shop-box').data('id')
					//通过id去遍历存在本地的所有cart里的每一个商品，把id不相同的保留下来
					$(this).parents('.shop-box').remove()

					_this.cart = _this.cart.filter((shop) => {
						return shop.id !== id
					})
					localStorage.setItem('cart', JSON.stringify(_this.cart))
					_this.setAllCheckStatus()
					_this.calcTotalMoney()
					_this.checksChange()
					let le = localStorage.getItem('cart')
					le = JSON.parse(le)
					if (le.length === 0) {
						localStorage.removeItem('cart')
						_this.hideee()
					}
				})
			}

			add() {
				const _this = this
				$('.btn-add').on('click', function() {
					const id = $(this).parents('.shop-box').data('id')
					let num = $(this).siblings('input').val()
					let cartnum = $('.ToCart').html()
					num++
					cartnum++
					$(this).siblings('input').val(num)
					$('.ToCart').html(cartnum)
					const price = $(this).parents('.shop-box').find('.now-price').html()
					$(this).parents('.shop-box').find('.nowTotal').html((num * price).toFixed(2))
					_this.cart = _this.cart.map((shop) => {
						if (shop.id === id) {
							shop.count++
						}
						return shop
					})
					localStorage.setItem('cart', JSON.stringify(_this.cart))
					_this.calcTotalMoney()
					Header.calcCartCount()
				})
			}

			minus() {
				const _this = this
				$('.btn-reduce').on('click', function() {
					const id = $(this).parents('.shop-box').data('id')
					let num = $(this).siblings('input').val()
					let cartnum = $('.ToCart').html()
					num--
					cartnum--
					if (num <= 1) {
						num = 1
						alert('亲，保留一件嘛，爱你哦！')
					}
					if (cartnum <= 1) {
						cartnum <= 1
					}
					$(this).siblings('input').val(num)
					$('.ToCart').html(cartnum)
					const price = $(this).parents('.shop-box').find('.now-price').html()
					$(this).parents('.shop-box').find('.nowTotal').html((num * price).toFixed(2))
					if (num=1) {
						_this.cart = _this.cart.map((shop) => {
							if (shop.id === id) {
								shop.count=1
							}
							return shop
						})
					}else{
						_this.cart = _this.cart.map((shop) => {
							if (shop.id === id) {
								shop.count--
							}
							return shop
						})
					}
					localStorage.setItem('cart', JSON.stringify(_this.cart))
					_this.calcTotalMoney()
					Header.calcCartCount()
				})
			}



		}
		return new Cart()
	})
})
